local ret = {}

local DIY = require "packages.diy_utility.diy_utility"

--- 判断蓄势状态不同
---@param player Player
---@param to Player
---@return boolean
local function compareReadySkillState(player, to)
  if player == to then return false end
  local states = {}
  for _, s in ipairs(player:getAllSkills()) do
    if s.visible and s:hasTag(DIY.ReadySkill) then
      table.insertIfNeed(states, DIY.isReadying(player, s))
    end
  end
  if #states == 0 then return false end
  local to_states = {}
  for _, s in ipairs(to:getAllSkills()) do
    if s.visible and s:hasTag(DIY.ReadySkill) then
      table.insertIfNeed(to_states, DIY.isReadying(to, s))
    end
  end
  if #to_states == 0 then return false end
  return #states > 1 or #to_states > 1 or states[1] ~= to_states[1]
end

for loop = 0, 30, 1 do

  local skel = fk.CreateSkill {
    name = loop == 0 and "rmt__guijian" or "rmt"..loop.."__guijian",
    tags = {Skill.Compulsory},
  }

  skel:addEffect(fk.DamageCaused, {
    priority = 1 - loop / 1000, -- 为了不重复询问
    mute = true,
    can_trigger = function(self, event, target, player, data)
      if player:hasSkill(skel.name) and target == player then
        return compareReadySkillState(player, data.to)
      end
    end,
    on_use = function (self, event, target, player, data)
      player.room:notifySkillInvoked(player, "rmt__guijian", "offensive")
      player:broadcastSkillInvoke("rmt__guijian", 1)
      data:changeDamage(1)
    end,
  })

  skel:addEffect(fk.DamageInflicted, {
    priority = 1 - loop / 1000,
    mute = true,
    can_trigger = function(self, event, target, player, data)
      if player:hasSkill(skel.name) and target == player then
        return data.from and compareReadySkillState(player, data.from)
      end
    end,
    on_use = function (self, event, target, player, data)
      player.room:notifySkillInvoked(player, "rmt__guijian", "defensive")
      player:broadcastSkillInvoke("rmt__guijian", 2)
      data:changeDamage(-1)
    end,
  })

  Fk:loadTranslationTable{
    [skel.name] = "归剑",
    [":"..skel.name] = "锁定技，你造成/受到伤害时，若其蓄势状态与你不同，伤害+1/-1。",
  }

  table.insert(ret, skel)
end

Fk:loadTranslationTable{
  ["$rmt__guijian1"] = "剑斩世间不平事，浮云深处藏功名。",
  ["$rmt__guijian2"] = "生者为过客，死者为归人。天地一逆旅，同悲万古尘。",
}

return ret
